<template>
  <div class="app-container falseTabel-container">
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select
            placeholder="选择品牌"
            v-model="formInline.brand_id"
            filterable
            @change="brandChange"
          >
            <el-option v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.name"
            placeholder="请输入套餐名称"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
        <el-button class="pull-right" type="primary" size="small" @click="addEditClick()">新增扫码套餐</el-button>
      </el-form>
      <!-- 表格 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="套餐名称" prop="name" width="210px"></el-table-column>
        <el-table-column align="center" label="页面按钮数" prop="button_num" width="100px"></el-table-column>
        <el-table-column align="center" label="扫码金额对应游玩次数" prop="text"></el-table-column>
        <el-table-column align="center" label="操作" width="220px">
          <template slot-scope="scope">
            <el-button
              @click="addEditClick(scope.row.id,scope.$index)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增编辑弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        id="selectStyle_parent"
        width="500px"
        :title="dialogTitle"
        :visible.sync="addEditDialog"
        append-to-body
        @close="callOf"
      >
        <div style="height:480px;overflow:auto;">
          <el-form :model="saveForm" ref="saveForm" :inline="true" class="demo-form-inline">
            <el-form-item label="套餐名称　" required>
              <el-input
                :maxlength="15"
                onkeypress="return event.which!=32"
                placeholder="请输入套餐名称(十五字内)"
                v-model="saveForm.name"
                style="width:350px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="所属品牌　" required>
              <el-select
                placeholder="选择品牌"
                v-model="saveForm.brand_id"
                style="width:350px"
                :disabled="isDisabled"
              >
                <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="页面按钮数" required>
              <el-select
                placeholder="选择页面按钮数"
                v-model="saveForm.button_num"
                style="width:350px"
                @change="button_numChange"
                :disabled="isDisabled"
              >
                <el-option label="1" value="1"></el-option>
                <el-option label="2" value="2"></el-option>
                <el-option label="3" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-table
                highlight-current-row
                :data="saveForm.rules"
                v-loading.body="listLoading"
                border
                size="small"
              >
                <el-table-column align="center" label="按钮" width="80">
                  <template slot-scope="scope">
                    <span class="indexColor">按钮{{scope.$index+1}}</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="扫码金额（元）" width="140">
                  <template slot-scope="scope">
                    <el-input
                      size="small"
                      onkeypress="return event.which>=48&&event.which<=57"
                      type="number"
                      onmousewheel="return false;"
                      min="0"
                      v-model="scope.row.money"
                      placeholder="输入正整数"
                      :disabled="isDisabled"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="游玩次数（次）" width="140">
                  <template slot-scope="scope">
                    <el-input
                      size="small"
                      onkeypress="return event.which>=48&&event.which<=57"
                      type="number"
                      onmousewheel="return false;"
                      min="0"
                      v-model="scope.row.times"
                      placeholder="输入正整数"
                      :disabled="isDisabled"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="首单赠送">
                  <template slot-scope="scope">
                    <el-checkbox v-model="scope.row.is_give" :disabled="isDisabled"></el-checkbox>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="savePackageClick" size="small">保 存</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getList,
  getPage,
  delPackage,
  savePackage,
  getStoreList,
  getBrandList,
  saveStore,
} from "@/api/member/scan_package";
export default {
  data() {
    return {
      list: null,
      listLoading: false,
      addEditDialog: false,
      isDisabled: true,
      storeList: null,
      brandList: null,
      editIndex: "",
      isEdit: false,
      currentPage: 1,
      url: "",
      dialogTitle: "",
      totalDataNumber: 1,
      formInline: {
        type: 0,
        name: "",
        brand_id: "",
      },
      saveForm: {
        brand_id: "",
        button_num: "",
        id: "",
        name: "",
        rules: [],
      },
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.brandList) {
        vm.getBrand();
      }
    });
  },
  methods: {
    button_numChange() {
      this.saveForm.rules = [];
      switch (this.saveForm.button_num) {
        case "1":
          this.saveForm.rules.push({
            money: "",
            times: "",
            times_first: "",
            is_give: false,
          });
          break;
        case "2":
          this.saveForm.rules.push(
            { money: "", times: "", times_first: "", is_give: false },
            { money: "", times: "", times_first: "", is_give: false }
          );
          break;
        case "3":
          this.saveForm.rules.push(
            { money: "", times: "", times_first: "", is_give: false },
            { money: "", times: "", times_first: "", is_give: false },
            { money: "", times: "", times_first: "", is_give: false }
          );
          break;
      }
    },
    getBrand() {
      getBrandList({ org: 1, category_id: 253 }).then((response) => {
        this.brandList = response;
        this.formInline.brand_id = response[0].id;
        if (this.list == null) {
          this.fetchData();
        }
      });
    },
    brandChange() {
      this.fetchData();
    },
    addEditClick(id, index) {
      this.dialogTitle = id ? "修改扫码套餐" : "添加扫码套餐";
      this.addEditDialog = true;
      this.editIndex = id ? index : "";
      this.isDisabled = id ? true : false;
      this.isEdit = id ? false : true;
      this.url = id
        ? "/Operations_sweep_package/edit_sweep_package"
        : "/Operations_sweep_package/create_sweep_package";
      this.saveForm.id = id ? id : "";
      this.saveForm.brand_id = id ? this.formInline.brand_id : "";
      this.saveForm.button_num = id ? this.list[index].button_num : "1";
      this.saveForm.name = id ? this.list[index].name : "";
      this.saveForm.rules = id
        ? this.list[index].rules
        : [{ money: "", times: "", times_first: "", is_give: false }];
    },
    savePackageClick() {
      let ok = true;
      if (
        this.saveForm.name == "" ||
        this.saveForm.brand_id == "" ||
        this.saveForm.button_num == ""
      ) {
        this.$message({ type: "error", message: "请将信息填写完整" });
        ok = false;
        return;
      }
      if (this.saveForm.rules) {
        this.saveForm.rules.forEach((val) => {
          if (
            val.money * 1 <= 0 ||
            val.times * 1 <= 0 ||
            val.times * 1 > 999999 ||
            val.times * 1 > 999999
          ) {
            this.$message({
              type: "error",
              message:
                "请填写完整充值金额和游玩次数，并且是只能为小于999999的正整数",
            });
            ok = false;
            return false;
          }
        });
      }
      if (ok) {
        this.$confirm(
          "套餐修改后将应用于所配置的全部机台，是否保存此次修改？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          this.saveForm.rules.forEach((val) => {
            val.times_first = val.is_give ? val.times * 1 + 1 : val.times;
          });
          let data = JSON.parse(JSON.stringify(this.saveForm));
          console.log(this.saveForm);
          savePackage(data, this.url).then((response) => {
            this.addEditDialog = false;
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
          });
        });
      }
    },
    callOf() {
      this.handleCurrentChange(this.currentPage);
    },
    // 搜索
    searchClick() {
      this.fetchData();
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response * 20;
          getList(this.formInline).then((response) => {
            response.forEach((val1) => {
              val1.rules.forEach((val2) => {
                val2.is_give =
                  val2.times_first * 1 > val2.times * 1 ? true : false;
              });
            });
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        response.forEach((val1) => {
          val1.rules.forEach((val2) => {
            val2.is_give = val2.times_first * 1 > val2.times * 1 ? true : false;
          });
        });
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>